DECLARE @T AS TABLE ([State] CHAR(2), CalendarQuarter INT, SavingsGroup INT, NetCostSavings MONEY)

INSERT  INTO @T 
SELECT  'ID' AS [State], 1 AS CalendarQuarter,1 AS SavingsGroup,'2.00' AS NetCostSavings
UNION ALL	 
--SELECT  'ID' AS [State], 1 AS CalendarQuarter,2 AS SavingsGroup,'2.00' AS NetCostSavings
--UNION ALL	
SELECT  'ID' AS [State], 1 AS CalendarQuarter,3 AS SavingsGroup,'2.00' AS NetCostSavings
UNION ALL	
SELECT  'ID' AS [State], 1 AS CalendarQuarter,4 AS SavingsGroup,'2.00' AS NetCostSavings
UNION ALL	
SELECT  'ID' AS [State], 1 AS CalendarQuarter,5 AS SavingsGroup,'2.00' AS NetCostSavings
UNION ALL	
SELECT  'ID' AS [State], 1 AS CalendarQuarter,6 AS SavingsGroup,'2.00' AS NetCostSavings
UNION ALL	
SELECT  'ID' AS [State], 1 AS CalendarQuarter,7 AS SavingsGroup,'12.00' AS NetCostSavings
UNION ALL	
SELECT  'ID' AS [State], 2 AS CalendarQuarter,1 AS SavingsGroup,'3.00' AS NetCostSavings
UNION ALL	
SELECT  'ID' AS [State], 2 AS CalendarQuarter,2 AS SavingsGroup,'3.00' AS NetCostSavings
UNION ALL	
SELECT  'ID' AS [State], 2 AS CalendarQuarter,3 AS SavingsGroup,'3.00' AS NetCostSavings
UNION ALL	
SELECT  'ID' AS [State], 2 AS CalendarQuarter,4 AS SavingsGroup,'3.00' AS NetCostSavings
UNION ALL	
SELECT  'ID' AS [State], 2 AS CalendarQuarter,5 AS SavingsGroup,'3.00' AS NetCostSavings
UNION ALL	
SELECT  'ID' AS [State], 2 AS CalendarQuarter,6 AS SavingsGroup,'3.00' AS NetCostSavings
UNION ALL	
SELECT  'ID' AS [State], 2 AS CalendarQuarter,7 AS SavingsGroup,'18.00' AS NetCostSavings
UNION ALL
SELECT  'WA' AS [State], 1 AS CalendarQuarter,1 AS SavingsGroup,'2.00' AS NetCostSavings
UNION ALL	 
SELECT  'WA' AS [State], 1 AS CalendarQuarter,2 AS SavingsGroup,'2.00' AS NetCostSavings
UNION ALL	
SELECT  'WA' AS [State], 1 AS CalendarQuarter,3 AS SavingsGroup,'2.00' AS NetCostSavings
UNION ALL	
SELECT  'WA' AS [State], 1 AS CalendarQuarter,4 AS SavingsGroup,'2.00' AS NetCostSavings
UNION ALL	
SELECT  'WA' AS [State], 1 AS CalendarQuarter,5 AS SavingsGroup,'2.00' AS NetCostSavings
UNION ALL	
SELECT  'WA' AS [State], 1 AS CalendarQuarter,6 AS SavingsGroup,'2.00' AS NetCostSavings
UNION ALL	
SELECT  'WA' AS [State], 1 AS CalendarQuarter,7 AS SavingsGroup,'12.00' AS NetCostSavings
UNION ALL	
SELECT  'WA' AS [State], 2 AS CalendarQuarter,1 AS SavingsGroup,'3.00' AS NetCostSavings
UNION ALL	
SELECT  'WA' AS [State], 2 AS CalendarQuarter,2 AS SavingsGroup,'3.00' AS NetCostSavings
UNION ALL	
SELECT  'WA' AS [State], 2 AS CalendarQuarter,3 AS SavingsGroup,'3.00' AS NetCostSavings
UNION ALL	
SELECT  'WA' AS [State], 2 AS CalendarQuarter,4 AS SavingsGroup,'3.00' AS NetCostSavings
UNION ALL	
SELECT  'WA' AS [State], 2 AS CalendarQuarter,5 AS SavingsGroup,'3.00' AS NetCostSavings
UNION ALL	
SELECT  'WA' AS [State], 2 AS CalendarQuarter,6 AS SavingsGroup,'3.00' AS NetCostSavings
UNION ALL	
SELECT  'WA' AS [State], 2 AS CalendarQuarter,7 AS SavingsGroup,'18.00' AS NetCostSavings



;WITH
DistinctStates AS --Grabs each unique state and quarter
(   SELECT  DISTINCT [State], CalendarQuarter
    FROM	  @T
),
FixedSavingsGroups AS --Gets all savings groups (columns for SSRS report)
(
    SELECT 1 AS SavingsGroup
    UNION SELECT 2
    UNION SELECT 3
    UNION SELECT 4
    UNION SELECT 5
    UNION SELECT 6
    UNION SELECT 7
),
PlaceholdFix AS
(
    SELECT  *
    FROM	  FixedSavingsGroups AS FSG
		  CROSS JOIN DistinctStates AS DSG
),
Data AS
(
    SELECT  COALESCE(T.[State],P.[State]) AS [State],
		  COALESCE(T.CalendarQuarter,P.CalendarQuarter) AS CalendarQuarter,
		  COALESCE(T.SavingsGroup,P.SavingsGroup) AS SavingsGroup,
		  COALESCE(T.NetCostSavings, 0) AS NetCostSavings
    FROM	  PlaceholdFix AS P
		  LEFT JOIN @T AS T ON (P.[State] = T.[State]
						    AND P.CalendarQuarter = T.CalendarQuarter
						    AND P.SavingsGroup = T.SavingsGroup)
		  
)
SELECT  [State],
	   [CalendarQuarter],
	   [1] AS 'PCP PRACTITIONAR & AMBULATORY SURGICAL CENTER SERVICES',
	   [2] AS 'PCP OUTPATIENT HOSPITAL SERVICES',
	   [3] AS 'PCP DURABLE MEDICAL EQUIPMENT',
	   [4] AS 'MU PRACTITIONAR & AMBULATORY SURGICAL CENTER SERVICES',
	   [5] AS 'MU OUTPATIENT HOSPITAL SERVICES',
	   [6] AS 'MU DURABLE MEDICAL EQUIPMENT',
	   [7] AS 'TOTAL SAVINGS'
FROM	   Data PIVOT (MAX(NetCostSavings) FOR SavingsGroup IN ([1],[2],[3],[4],[5],[6],[7])) AS PivotTable
ORDER   BY [State], CalendarQuarter




		  